<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备名称" prop="eqName">
        <el-input
          v-model="queryParams.eqName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备型号" prop="eqModel">
        <el-input
          v-model="queryParams.eqModel"
          placeholder="请输入设备型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备归属" prop="eqHome">
        <el-input
          v-model="queryParams.eqHome"
          placeholder="请输入设备归属地"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['equipment:equipment:export']"
            >导出</el-button>
          </el-col>
    <!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
        </el-row>

    <el-table v-loading="loading" :data="equipmentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
<!--      <el-table-column label="主键" align="center" prop="id"/>-->
      <el-table-column label="设备id" align="center" prop="eqId"/>
      <el-table-column label="设备名称" align="center" prop="eqName"/>
      <el-table-column label="设备型号" align="center" prop="eqModel"/>
      <el-table-column label="设备归属地" align="center" prop="eqHome"/>
      <el-table-column label="入库人" align="center" prop="stUser"/>

      <el-table-column label="设备状态" align="center" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status === 1" style="color: blue;">正常</span>
          <span v-else-if="scope.row.status === 0" style="color: red;">报废</span>
          <span v-else-if="scope.row.status === 2" style="color: yellow;">报废审核中</span>
          <span v-else-if="scope.row.status === 3" style="color: black;">维修中</span>
<!--          <span v-else style="color: gray;">未知</span>-->
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDetails(scope.row)"
            v-hasPermi="['equipment:equipment:remove']"
          >查看详情
          </el-button>
          <el-dialog title="设备详情" :visible.sync="detailDialogVisible" width="500px">
            <div style="text-align: center;">
              <el-image
                src="@/assets/images/b1.png"
                :preview-src-list="[detailImageUrl]"
                v-if="detailImageUrl"
                style="max-width: 100%; max-height: 400px;"
              >
<!--                <div slot="error" class="image-slot">-->
<!--                  加载失败-->
<!--                </div>-->
              </el-image>
              <p v-else>暂无图片</p>
            </div>
          </el-dialog>


          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete" style="color: red;"
            @click="deleteDevice(scope.row)"
            v-hasPermi="['judge:erequest:remove']"
          >请求报废设备
          </el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改equipment对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>


export default {
  name: "Equipment",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      detailDialogVisible: false, // 控制详情弹窗
      detailImageUrl: '', // 图片地址
      // equipment表格数据
      equipmentList: [
        {
          "createBy": null,
          "createTime": null,
          "updateBy": null,
          "updateTime": null,
          "remark": null,
          "id": 93,
          "eqId": "7387239758671384576",
          "eqName": "全自动11",
          "currentReviewerSort": 1,
          "reviewerStatus": "{\"sort_2\":null,\"sort_1\":null}",
          "totalReviewers": 2,
          "pass": null,
          "eqModel": "BACTEC FX",
          "other": "",
          "other2": null,
          "status": 1,
          "eqHome": "外科",
          "stUser": "医院管理员",
          "stTime": "2025-10-24 05:33:27"
        },
        {
          "createBy": null,
          "createTime": null,
          "updateBy": null,
          "updateTime": null,
          "remark": null,
          "id": 94,
          "eqId": "7387243430243471360",
          "eqName": "全自动细菌培养系统",
          "currentReviewerSort": 0,
          "reviewerStatus": "{\"sort_2\":\"Y\",\"sort_1\":\"Y\"}",
          "totalReviewers": 2,
          "pass": "Y",
          "eqModel": "BACTEC FX",
          "other": "",
          "other2": null,
          "status": 0,
          "eqHome": "内科",
          "stUser": "医院管理员",
          "stTime": "2025-10-24 05:48:02"
        },
        {
          "createBy": null,
          "createTime": null,
          "updateBy": null,
          "updateTime": null,
          "remark": null,
          "id": 95,
          "eqId": "7388923180712202240",
          "eqName": "滋场刺激仪",
          "currentReviewerSort": 1,
          "reviewerStatus": "{\"sort_2\":null,\"sort_1\":null}",
          "totalReviewers": 2,
          "pass": null,
          "eqModel": "MagTD",
          "other": "",
          "other2": null,
          "status": 1,
          "eqHome": "外科",
          "stUser": "医院管理员",
          "stTime": "2025-10-28 21:02:46"
        }
      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        eqId: null,
        eqName: null,
        eqModel: null,
        other: null,
        eqHome: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  activated() {
    this.getList();
  },
  beforeRouteEnter(to, from, next) {
    next(vm => {
      vm.getList();
    });
  },
  watch: {
    '$route': 'getList'
  },
  methods: {
    /** 查询equipment列表 */
    getList() {
      this.loading = false;
      // listEquipment(this.queryParams).then(response => {
      //   this.equipmentList = response.rows;
      //   console.log("设备列表",this.equipmentList);
      //   this.total = response.total;
      //
      // });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        eqId: null,
        eqName: null,
        eqModel: null,
        other: null,
        eqHome: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 报废设备按钮操作 */
    deleteDevice(row) {
      const id = row.eqId;
      this.$modal.confirm('是否确认请求报废编号为"' + id + '"的设备？')
        .then(() => {
          return BrokenRequest(id); // 调用报废API
        })
        .then(() => {
          this.getList(); // 刷新列表
          this.$modal.msgSuccess("请求成功");
        })
        .catch(() => {
        });
    },


    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加equipment";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      this.open = true;
      this.title = "修改equipment";
    },

    /** 查看详情按钮操作 */
    handleDetails(row) {
      // 根据 row 中的字段决定显示哪张图，这里假设你有一个字段叫 imagePath 表示图片名称
      const imageName = row.other || 'device1.jpg'; // 默认图
      console.log(imageName);
      // 拼接路径并 require 加载
      // this.detailImageUrl = require(`@/assets/images/${imageName}`);
      this.detailImageUrl = imageName;

      this.detailDialogVisible = true;
    },


    /** 提交按钮 */
    submitForm() {
      // this.$refs["form"].validate(valid => {
      //   if (valid) {
      //     if (this.form.id != null) {
      //       updateEquipment(this.form).then(response => {
      //         this.$modal.msgSuccess("修改成功");
      //         this.open = false;
      //         this.getList();
      //       });
      //     } else {
      //       addEquipment(this.form).then(response => {
      //         this.$modal.msgSuccess("新增成功");
      //         this.open = false;
      //         this.getList();
      //       });
      //     }
      //   }
      // });
    },
    /** 报废按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认报废equipment编号为"' + ids + '"的数据项？').then(function () {
        // return delEquipment(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("报废成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('equipment/equipment/export', {
        ...this.queryParams
      }, `equipment_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
